<template>
  <div class="crrSupplementRecordreport-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="选择日期" prop="recordDate">
          <div style="display: flex; align-items: center">
            <el-button-group style="display: flex; align-items: center">
              <el-button type="primary">当天</el-button>
              <el-button>其他日期</el-button>
            </el-button-group>
            <el-input
              style="margin-left: 6px"
              placeholder="请输入选择日期"
              v-model="searchForm.recordDate"
              autocomplete="off"
            />
          </div>
        </el-form-item>
        <el-form-item label="选择门店" prop="storeName">
          <el-input
            placeholder="请输入门店名称"
            v-model="searchForm.storeName"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item label="物料名称" prop="materialName">
          <el-input
            placeholder="请输入物料名称"
            v-model="searchForm.materialName"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
            <el-button @click="startClear" type="success"> 导出报表 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增补料记录
        </el-button>
      </div>

      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="storeName" label="门店名称" width="180" />
          <el-table-column prop="recordDate" label="日期" width="120" />
          <el-table-column
            prop="machineUniqueCode"
            label="机器唯一识别码"
            width="240"
          />
          <!-- <el-table-column prop="materialCode" label="物料编号" width="190" /> -->
          <el-table-column prop="materialName" label="物料名称" width="200" />
          <el-table-column
            prop="supplementCount"
            label="补料次数"
            width="100"
          />
          <el-table-column
            prop="totalSupplementQuantity"
            label="补料总量(g)"
            width="100"
          />
          <el-table-column label="操作" min-width="120px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <CrrSupplementRecordreportAdd
      v-model="addUpdateRef"
    ></CrrSupplementRecordreportAdd>
    <CrrSupplementRecordreportRemove
      v-model="removeRef"
    ></CrrSupplementRecordreportRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getCrrSupplementRecordreportPagination,
  getCrrSupplementRecordreportAllList,
} from '@/api/crr-supplement-recordreport.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import CrrSupplementRecordreportRemove from './CrrSupplementRecordreportRemove.vue'
import CrrSupplementRecordreportAdd from './CrrSupplementRecordreportAdd.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'
import { getStorePagination, getStoreAllList } from '@/api/tms-store.api'
import {
  getMaterialConfigAllList,
  getMaterialConfigPagination,
} from '@/api/fms-material-config.api'

const titleName = `补料记录报表表`
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `补料记录报表表`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  storeList: [],
  materialConfigList: [],
  refreshDataList: () => {
    getDataList()
  },
})

// const requestCanUseCode = () => {
// 	getCanUseCode({ type: 8 })
// 			.then((res: any) => {
// 				console.log('getCanUseCode: ', res)
// 				addUpdateRef.form.number = res
// 			})
// 			.catch((err) => {
// 				ElMessage.error(err.msg)
// 			})
// }

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      storeName: '',
      recordDate: '',
      machineUniqueCode: '',
      materialCode: '',
      materialName: '',
      supplementCount: '',
      totalSupplementQuantity: '',
      disabled: false,
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    // requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    storeName: item.storeName,
    recordDate: item.recordDate,
    machineUniqueCode: item.machineUniqueCode,
    materialCode: item.materialCode,
    materialName: item.materialName,
    supplementCount: item.supplementCount,
    totalSupplementQuantity: item.totalSupplementQuantity,
    disabled: false,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `补料记录报表表`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  storeName: '',
  recordDate: '',
  machineUniqueCode: '',
  materialCode: '',
  materialName: '',
  supplementCount: '',
  totalSupplementQuantity: '',
})

const tableLoading = ref(false)
const tableData = ref([])

/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let storeName = searchForm.value.storeName
  if (storeName) {
    searchCondition.storeName = storeName
  }
  let recordDate = searchForm.value.recordDate
  if (recordDate) {
    searchCondition.recordDate = recordDate
  }
  let machineUniqueCode = searchForm.value.machineUniqueCode
  if (machineUniqueCode) {
    searchCondition.machineUniqueCode = machineUniqueCode
  }
  let materialCode = searchForm.value.materialCode
  if (materialCode) {
    searchCondition.materialCode = materialCode
  }
  let materialName = searchForm.value.materialName
  if (materialName) {
    searchCondition.materialName = materialName
  }

  tableLoading.value = true
  getCrrSupplementRecordreportPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)

      let list = res.list || []
      list = list.map((item: any) => {
        item.recordDate = item.recordDate.substring(0, 11)
        return item
      })
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

/***
 * 获取门店
 */
const storeList = ref([])
const requestStoreAllList = () => {
  let formData = {}
  getStoreAllList(formData)
    .then((res: any) => {
      storeList.value = res || []
      addUpdateRef.storeList = storeList.value
    })
    .catch((err) => {
      console.log(err)
    })
}

const materialConfigList = ref([])
const requestMaterialConfigAllList = () => {
  let formData = {}
  getMaterialConfigAllList(formData)
    .then((res: any) => {
      materialConfigList.value = res || []
      addUpdateRef.materialConfigList = materialConfigList.value
    })
    .catch((err: any) => {
      console.log(err)
    })
}

onMounted(() => {
  requestMaterialConfigAllList()
  requestStoreAllList()
  getDataList()
})
</script>
<style lang="scss" scoped>
.crrSupplementRecordreport-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.crrSupplementRecordreport-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
